package seekting.demo2017.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.seekting.demo_lib.Demo;

import seekting.demo2017.R;
import seekting.demo2017.databinding.BroadcastLayoutBinding;

/**
 * Created by Administrator on 2017/8/26.
 */

@Demo(title = "有序广播", desc = "有序广播的发送形式及接收机制")
public class OrderBroadCastActivity extends Activity {
    private static final String ACTION = "OrderBroadCastActivity";
    private static final String PERMISSION = "seekting.demo2017.APP_PERMISSION";
    private BroadcastReceiver mReceiver;

    HandlerThread mHandlerThread = new HandlerThread("order_braodcast_thread");
    private Handler mHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("seekting", "OrderBroadCastActivity.onCreate()");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.d("seekting", "OrderBroadCastActivity.handleMessage()");
            }
        };
        BroadcastLayoutBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.broadcast_layout);
        viewDataBinding.setHandlers(this);

        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle a = getResultExtras(true);
                if (a != null) {
                    Log.d("seekting", "OrderBroadCastActivity.onReceive()" + a.get("seekting"));
                } else {
                    Log.d("seekting", "OrderBroadCastActivity.onReceive()");
                }


            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION);
        intentFilter.setPriority(10);
//        registerReceiver(mReceiver, intentFilter, PERMISSION, new Handler() {
//            @Override
//            public void handleMessage(Message msg) {
//                super.handleMessage(msg);
//                Log.d("seekting", "OrderBroadCastActivity.handleMessage()");
//            }
//        });
        registerReceiver(mReceiver, intentFilter);
    }

    public void onClick() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    public void onClickSend(View v) {

        Intent intent = new Intent(ACTION);
        sendOrderedBroadcast(intent, PERMISSION);
//        sendBroadcast(intent);
//
    }


    public void onClickSendWithHandler(View v) {
        Intent intent = new Intent(ACTION);
        Bundle initialExtras = new Bundle();
        initialExtras.putInt("seekting", 0);
        String initData = "initialData";
        int initialCode = 1;
        BroadcastReceiver resultReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("seekting", "resultReceiver.onReceive()");
                Log.d("seekting", "resultReceiver.onReceive()" + Thread.currentThread().getName());
                boolean isAbort = getAbortBroadcast();
                Log.d("seekting", "resultReceiver.onReceive()isAbort=" + isAbort);


            }
        };
        sendOrderedBroadcast(intent, PERMISSION, resultReceiver, mHandler, initialCode, initData, initialExtras);

    }
}
